package org.hzz.coupon.infrastructure.repository.mybatis;

import io.gitee.pkmer.convention.converter.RecordAndEntityConverter;
import io.gitee.pkmer.convention.page.PageResponse;
import io.gitee.pkmer.convention.page.query.PageQuery;
import io.gitee.pkmer.convention.repository.nomapper.BaseRepository;
import io.gitee.pkmer.mybatis.support.DynamicPageQuerySupport;
import io.gitee.pkmer.util.ReflectUtil;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.hzz.coupon.infrastructure.dao.mapper.coupon.CouponDynamicSqlSupport;
import org.hzz.coupon.infrastructure.dao.mapper.coupon.CouponScopeDynamicMapper;
import org.hzz.coupon.infrastructure.dao.mapper.coupon.CouponScopeDynamicSqlSupport;
import org.hzz.coupon.infrastructure.dao.mapper.coupon.CouponScopeMapper;
import org.hzz.coupon.infrastructure.dao.model.coupon.CouponScope;
import org.hzz.promotions.domain.entity.CouponScopeEntity;
import org.hzz.promotions.domain.repository.CouponScopeRepository;
import org.hzz.promotions.types.enums.coupon.CouponScopeType;
import org.hzz.promotions.types.enums.coupon.CouponStatus;
import org.hzz.promotions.types.enums.coupon.DiscountType;
import org.hzz.promotions.types.enums.coupon.ObtainWay;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import org.mybatis.dynamic.sql.AliasableSqlTable;
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Optional;

import static org.hzz.coupon.infrastructure.dao.mapper.coupon.CouponScopeDynamicSqlSupport.couponId;
import static org.hzz.coupon.infrastructure.dao.mapper.coupon.CouponScopeDynamicSqlSupport.couponScope;
import static org.mybatis.dynamic.sql.SqlBuilder.*;

/**
 * @author <a href="mailto:1193094618@qq.com">pkmer</a>
 * <br>
 * <a href = "https://gitee.com/developeros/videos-online">Code Repository</a>
 * At 2024/8/30
 *
 */
@Repository
@Slf4j
public class CouponScopeRepositoryImpl
        extends DynamicPageQuerySupport<CouponScopeDynamicSqlSupport, CouponScopeDynamicSqlSupport.CouponScope>
        implements CouponScopeRepository {

    @Setter(onMethod_ = @Autowired)
    private CouponScopeDynamicMapper dynamicMapper;

    @Setter(onMethod_ = @Autowired)
    private CouponScopeMapper mapper;

    @Override
    public void insertCouponScope(CouponScopeEntity entity) {
        mapper.insertSelective(Converter.INSTANCE.toRecord(entity));
    }

    @Override
    public void batchInsertCouponScope(List<CouponScopeEntity> entities) {
        List<CouponScope> records = Converter.INSTANCE.toRecords(entities);
        dynamicMapper.insertMultiple(records);
    }

    @Override
    public void deleteCouponScopeByCouponId(Long _couponId) {
        DeleteStatementProvider deleteStatement = deleteFrom(couponScope)
                .where(couponId,isEqualTo(_couponId))
                .build()
                .render(RenderingStrategies.MYBATIS3);

        log.info(deleteStatement.getDeleteStatement());
        dynamicMapper.delete(deleteStatement);
    }

    @Override
    public List<CouponScopeEntity> getCouponScopeByCouponId(Long _couponId) {

        SelectStatementProvider selectStatementProvider = select(couponScope.allColumns())
                .from(couponScope)
                .where(couponId, isEqualTo(_couponId))
                .build()
                .render(RenderingStrategies.MYBATIS3);

        log.info(selectStatementProvider.getSelectStatement());
        List<CouponScope> records = dynamicMapper.selectMany(selectStatementProvider);
        return Converter.INSTANCE.toEntities(records);
    }

    @SuppressWarnings("unchecked")
    public PageResponse<CouponScopeEntity> pageQueryCouponScope(PageQuery pageQuery){

        SelectStatementProvider selectStatementProvider = select(couponScope.allColumns())
                .from(couponScope)
                //.applyWhere(whereApplier)
                .limit(pageQuery.getPageSize())
                .offset((long) pageQuery.getPageSize() * (pageQuery.getPageNo() - 1))
                .build()
                .render(RenderingStrategies.MYBATIS3);

        super.pageQuery(pageQuery, dynamicMapper,CouponScopeEntity.class);
        return null;
    }

    @Mapper
    interface Converter extends RecordAndEntityConverter<CouponScope,CouponScopeEntity> {
        Converter INSTANCE = Mappers.getMapper(Converter.class);
    }
}
